#include "BackGroundShadow.h"
#include "LoadResource.h"
using namespace cocos2d;

char shadowChar[] = "abcdefghijklmnopqrst";
BackGroundShadow::BackGroundShadow(std::string string):cstring(string),pngstring(".png")
{
	this->init();
	this->initData();
	this->loadTexture();
	this->initPosition();
}


BackGroundShadow::~BackGroundShadow(void)
{
}

void BackGroundShadow::loadTexture()
{
LoadTextureResource *load = LoadTextureResource::getShare();
	//CCTexture2D * texture[SHADOWSPRITES];
	std::string recourceString;
	//for (int i =0;i<SHADOWSPRITES;i++)
	//{
	//	recourceString = cstring+shadowChar[i]+pngstring;
	//	texture[i] = CCTextureCache::sharedTextureCache()->addImage(recourceString.c_str());
	//}
	//float sx = winSize.width/texture[0]->getContentSize().width;
	//float sy = winSize.height/texture[0]->getContentSize().height;
	for (int i = 0;i<SHADOWSPRITES;i++)
	{
		sprites[i] = CCSprite::createWithTexture(load->backgroundShadowTexture[i]);
		sprites[i]->setScaleX(icc->getwholeScaleX());
		sprites[i]->setScaleY(icc->getwholeScaleY());
		sprites[i]->setVisible(false);
		this->addChild(sprites[i]);
	}
}

void BackGroundShadow::initData()
{
	winSize = CCDirector::sharedDirector()->getWinSize();
	this->setContentSize(CCSize::CCSize(winSize.width*2,winSize.height));	
	showList = new struct ShowList;
	showList->pSprite = NULL;
	showList->next = new struct ShowList;
	showList->next->pSprite = NULL;
	showList->next->next= NULL;

	icc= ICC::getShareIcc();
}

void BackGroundShadow::initPosition()
{
	showList->pSprite = sprites[0];
	showList->next->pSprite = sprites[1];
	showList->pSprite->setVisible(true);
	showList->pSprite->setPosition(ccp(winSize.width/2,winSize.height/2));

	showList->next->pSprite->setVisible(true);
	showList->next->pSprite->setPosition(ccp(winSize.width*1.5f,winSize.height/2));
}

void BackGroundShadow::draw()
{
	showList->pSprite->setPositionX(showList->pSprite->getPositionX()-icc->getBackValue()*PTM_RATIO*0.3);
	showList->next->pSprite->setPositionX(showList->next->pSprite->getPositionX()-icc->getBackValue()*PTM_RATIO*0.3);
	if (showList->pSprite->getPositionX() <= (0-winSize.width/2))
	{
		float n = (0-winSize.width/2) - showList->pSprite->getPositionX();
		updateShowSprites(showList,n);
	} 
	else if (showList->next->pSprite->getPositionX() <= (0-winSize.width/2))
	{
		float n = (0-winSize.width/2) - showList->next->pSprite->getPositionX();
		updateShowSprites(showList->next,n);
	}
}

void BackGroundShadow::updateShowSprites( struct ShowList *pShow,float n )
{
	pShow->pSprite->setPosition(ccp(0,0));
	pShow->pSprite->setVisible(false);
	pShow->pSprite = getNextSprite();
	pShow->pSprite->setVisible(true);
	pShow->pSprite->setPosition(ccp(winSize.width*1.5f-n,winSize.height/2));
}

cocos2d::CCSprite * BackGroundShadow::getNextSprite()
{
	int j;
	do 
	{
		j = rand()%(SHADOWSPRITES);
	} while (sprites[j] == showList->pSprite || sprites[j] ==showList->next->pSprite);
	return sprites[j];
}

